<?php
class Footermenu_AdminSettingsController extends Core_Controller_Action_Admin {
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_settings' );
		
		$form = $this->view->form = new Footermenu_Form_Admin_Settings ();
		
		$page = $this->_getParam ( 'page', 1 );
		
		$table = $this->_helper->api ()->getDbtable ( 'collections', 'footermenu' );
		$select = $table->select ();
		
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
			
			$select = $table->select ()->order ( 'collection_id ASC' );
		
		}
		
		$this->view->paginator = $paginator = Zend_Paginator::factory ( $select );
		$paginator->setItemCountPerPage ( 20 );
		$paginator->setCurrentPageNumber ( $page );
	}
	
	public function createAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_settings' );
		
		$this->view->form = $form = new Footermenu_Form_Admin_Create ();
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			$table = $this->_helper->api ()->getDbtable ( 'collections', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$collection = $table->createRow ();
				$collection->setFromArray ( $values );
				$collection->save ();
				
				// Commit
				$db->commit ();
				
				// Redirect
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
			
		//$this->_helper->redirector->gotoRoute(array());
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		
		}
	}
	
	public function editAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_settings' );
		
		$this->view->form = $form = new Footermenu_Form_Admin_Edit ();
		
		$id = $this->_getParam ( 'id', null );
		
		$collection = Engine_Api::_ ()->getItem ( 'footermenu_collection', $id );
		$form->populate ( $collection->toArray () );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			$table = $this->_helper->api ()->getDbtable ( 'collections', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				//$collection = $table->createRow();
				$collection->setFromArray ( $values );
				$collection->save ();
				
				// Commit
				$db->commit ();
				
				// Redirect
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
			
		//$this->_helper->redirector->gotoRoute(array());
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		
		}
	}
	
	public function deleteAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_settings' );
		
		$this->view->form = $form = new Footermenu_Form_Admin_Delete ();
		$id = ( int ) $this->_getParam ( 'id', null );
		
		$this->view->collection = $collection = Engine_Api::_ ()->getItem ( 'footermenu_collection', $id );
		
		if ($id) {
			$form->collection_id->setValue ( $id );
		}
		
		if ($this->getRequest ()->isPost ()) {
			$table = $this->_helper->api ()->getDbtable ( 'collections', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				// remove all cat & links associated with this collection
				$collection->removeAllAssoc ( $id );
				
				// delete collection
				$collection->delete ();
				
				// commit
				$db->commit ();
				
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		}
	}
	
	public function deleteselectedAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_settings' );
		
		$this->view->ids = $ids = $this->_getParam ( 'ids', null );
		$confirm = $this->_getParam ( 'confirm', false );
		$this->view->count = count ( explode ( ",", $ids ) );
		
		// $announcement = Engine_Api::_()->getItem('announcement', $id);
		

		// Save values
		if ($this->getRequest ()->isPost () && $confirm == true) {
			$ids_array = explode ( ",", $ids );
			
			foreach ( $ids_array as $id ) {
				$collection = Engine_Api::_ ()->getItem ( 'footermenu_collection', $id );
				
				// remove all permissions associated with this levle
				$collection->removeAllAssoc ( $id );
				
				// delete level
				$collection->delete ();
			
			}
			
			//$announcement->delete();
			return $this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
		}
	}
	
	public function categoryAction() {
		$id = ( int ) $this->_getParam ( 'collection_id', 0 );
		if (! Engine_Api::_ ()->footermenu ()->Collection ())
			return $this->_helper->redirector->gotoRoute ( array ('action' => 'create' ) );
		
		if ($id == 0)
			$id = Engine_Api::_ ()->footermenu ()->CollectionFirst ();
		
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_category' );
		
		$form = $this->view->form = new Footermenu_Form_Admin_Category ();
		
		$page = $this->_getParam ( 'page', 1 );
		
		$table = $this->_helper->api ()->getDbtable ( 'categories', 'footermenu' );
		$categoriesTableName = $table->info ( 'name' );
		$select = $table->select ();
		
		if ($form->isValid ( $this->_getAllParams () )) {
			$values = $form->getValues ();
			
			$select = $table->select ()->order ( 'category_id ASC' );
		
		}
		$select->where ( "{$categoriesTableName}.cid = ?", $id );
		// prepare collections
		$table_coll = Engine_Api::_ ()->getDbtable ( 'collections', 'footermenu' );
		$select_coll = $table_coll->select ();
		$this->view->collections = $table_coll->fetchAll ( $select_coll );
		$this->view->collection_id = $id;
		
		$this->view->paginator = $paginator = Zend_Paginator::factory ( $select );
		$paginator->setItemCountPerPage ( 20 );
		$paginator->setCurrentPageNumber ( $page );
	}
	
	public function createcategoryAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_category' );
		
		$this->view->form = $form = new Footermenu_Form_Admin_Createcategory ();
		$id = $this->_getParam ( 'collection_id', 0 );
		if ($id == 0)
			$id = Engine_Api::_ ()->footermenu ()->CollectionFirst ();
		
		if ($id) {
			$form->cancel->setAttrib ( 'href', 'admin/footermenu/settings/category/collection_id/' . $id );
			$form->cid->setValue ( $id );
		}
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			$table = $this->_helper->api ()->getDbtable ( 'categories', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$collection = $table->createRow ();
				$collection->setFromArray ( $values );
				$collection->save ();
				
				// Commit
				$db->commit ();
				
				// Redirect
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'category' ) );
			
		//$this->_helper->redirector->gotoRoute(array());
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		
		}
	}
	
	public function editcategoryAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_category' );
		
		$this->view->form = $form = new Footermenu_Form_Admin_Editcategory ();
		$id = ( int ) $this->_getParam ( 'id', 0 );
		$cid = ( int ) $this->_getParam ( 'collection_id', 0 );
		
		if ($cid == 0)
			$cid = Engine_Api::_ ()->footermenu ()->CollectionFirst ();
		
		if ($id) {
			$form->cancel->setAttrib ( 'href', 'admin/footermenu/settings/category/collection_id/' . $cid );
			$form->cid->setValue ( $id );
		}
		$category = Engine_Api::_ ()->getItem ( 'footermenu_category', $id );
		$form->populate ( $category->toArray () );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			$table = $this->_helper->api ()->getDbtable ( 'categories', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$category->setFromArray ( $values );
				$category->save ();
				
				// Commit
				$db->commit ();
				
				// Redirect
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'category' ) );
			
		//$this->_helper->redirector->gotoRoute(array());
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		
		}
	}
	
	public function deletecategoryAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_category' );
		
		$this->view->form = $form = new Footermenu_Form_Admin_Deletecategory ();
		$id = ( int ) $this->_getParam ( 'id', 0 );
		$cid = ( int ) $this->_getParam ( 'collection_id', 0 );
		if ($cid == 0)
			$cid = Engine_Api::_ ()->footermenu ()->CollectionFirst ();
		
		$this->view->category = $category = Engine_Api::_ ()->getItem ( 'footermenu_category', $id );
		
		if ($id) {
			$form->category_id->setValue ( $id );
			$form->cancel->setAttrib ( 'href', 'admin/footermenu/settings/category/collection_id/' . $cid );
		}
		
		if ($this->getRequest ()->isPost ()) {
			$table = $this->_helper->api ()->getDbtable ( 'categories', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				// remove all permissions associated with this levle
				$category->removeAllLinks ( $id );
				
				// delete category
				$category->delete ();
				
				// commit
				$db->commit ();
				
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'category' ) );
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		}
	}
	
	public function deleteselectedcatAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_category' );
		
		$this->view->ids = $ids = $this->_getParam ( 'ids', null );
		$confirm = $this->_getParam ( 'confirm', false );
		$this->view->count = count ( explode ( ",", $ids ) );
		
		$cid = ( int ) $this->_getParam ( 'collection_id', 0 );
		if ($cid == 0)
			$cid = Engine_Api::_ ()->footermenu ()->CollectionFirst ();
		
		$this->view->cancel = 'admin/footermenu/settings/category/collection_id/' . $cid;
		
		// Save values
		if ($this->getRequest ()->isPost () && $confirm == true) {
			$ids_array = explode ( ",", $ids );
			
			foreach ( $ids_array as $id ) {
				$category = Engine_Api::_ ()->getItem ( 'footermenu_category', $id );
				
				$category->removeAllLinks ( $id );
				
				// delete cat
				$category->delete ();
			
			}
			
			//$announcement->delete();
			return $this->_helper->redirector->gotoRoute ( array ('action' => 'category' ) );
		}
	}
	
	public function addlinkAction() {
		$this->view->form = $form = new Footermenu_Form_Admin_Addlink ();
		$id = ( int ) $this->_getParam ( 'id', 0 );
		if ($id) {
			$form->cat_id->setValue ( $id );
		}
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			$table = $this->_helper->api ()->getDbtable ( 'references', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$link = $table->createRow ();
				$link->setFromArray ( $values );
				$link->save ();
				
				// Commit
				$db->commit ();
				
				// Forward
				return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array ('Link has been added.' ) ) );
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		
		}
	}
	
	public function editlinkAction() {
		$this->view->form = $form = new Footermenu_Form_Admin_Editlink ();
		$id = ( int ) $this->_getParam ( 'id', 0 );
		
		$link = Engine_Api::_ ()->getItem ( 'footermenu_reference', $id );
		$form->populate ( $link->toArray () );
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			
			$table = $this->_helper->api ()->getDbtable ( 'references', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$values = $form->getValues ();
				$link->setFromArray ( $values );
				$link->save ();
				
				// Commit
				$db->commit ();
				
				// Forward
				return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array ('Link has been edited.' ) ) );
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		
		}
	}
	
	public function deletelinkAction() {
		$this->view->form = $form = new Footermenu_Form_Admin_Deletelink ();
		$id = ( int ) $this->_getParam ( 'id', 0 );
		
		$link = Engine_Api::_ ()->getItem ( 'footermenu_reference', $id );
		
		if ($this->getRequest ()->isPost ()) {
			$table = $this->_helper->api ()->getDbtable ( 'references', 'footermenu' );
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				// delete link
				$link->delete ();
				
				// commit
				$db->commit ();
				
				// Forward
				return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array ('Link has been deleted.' ) ) );
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
		}
	}
	
	//More Plugins
	public function moreAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'footermenu_admin_main', array (), 'footermenu_admin_main_more' );
	}
}